package cn.yunyichina.provider.sb.service.impl;

import cn.yunyichina.provider.framework.data.SpringContextHandler;
import cn.yunyichina.provider.framework.dubbo.service.SbService;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.provider.sb.constant.Constant;
import cn.yunyichina.provider.sb.dao.MedicalPatientDao;
import cn.yunyichina.provider.sb.entity.MedicalPatient;
import cn.yunyichina.utils.convert.JsonUtils;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipayEncrypt;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 15-AK on 2017/11/2.
 */
public class TestServiceImpl {
    private static final Logger logger = LoggerFactory.getLogger(TestServiceImpl.class);
    private MedicalPatientDao medicalPatientDao = SpringContextHandler.getBean("medicalPatientDao");



    public void  alipayTestOutMedicalNotify() throws AlipayApiException {
        String method = "yyt.sb.notify.alipay";
        MedicalPatient queryMedicalPatient = new MedicalPatient();
        queryMedicalPatient.setOrgCode("szszyy");
        queryMedicalPatient.setBusinessType(new Short("1"));
        List<MedicalPatient> medicalPatientList = medicalPatientDao.selectLimitPatient(queryMedicalPatient);
        for (MedicalPatient medicalPatient : medicalPatientList){
            Response medicalResponse = SbService.callHumpFromInternal(method,encryptOutMedicalNotifyMessage(medicalPatient));
        }
    }

    public void  alipayTestInhospitalNotify() throws AlipayApiException {
        String method = "yyt.sb.notify.alipay";
        MedicalPatient queryMedicalPatient = new MedicalPatient();
        queryMedicalPatient.setOrgCode("szszyy");
        queryMedicalPatient.setBusinessType(new Short("2"));
        List<MedicalPatient> medicalPatientList = medicalPatientDao.selectLimitPatient(queryMedicalPatient);
        for (MedicalPatient medicalPatient : medicalPatientList){
            Response medicalResponse = SbService.callHumpFromInternal(method,encryptInhospitalNotifyMessage(medicalPatient));
        }
    }


    public static String encryptInhospitalNotifyMessage(MedicalPatient medicalPatient ) throws AlipayApiException {
        JSONObject info = new JSONObject();
        info.put("patient_id",medicalPatient.getPatId());
        info.put("hospital_id", "szszyy");
        info.put("visit_date",medicalPatient.getVisitTime());
        info.put("medical_type", "I");
        info.put("case_no", "88888888");
        info.put("serial_no", medicalPatient.getInpatientId());
        String beforeEncryptData = info.toJSONString();
        logger.info("模拟支付宝回调加密前数据beforeEncryptData={}",beforeEncryptData);
        String afterEncryptData = AlipayEncrypt.encryptContent(beforeEncryptData, "AES", Constant.ENCRYPT_KEY, "utf8");
        JSONObject result = new JSONObject();
        result.put("content",afterEncryptData);
        return  result.toJSONString();
    }

    public static String encryptOutMedicalNotifyMessage(MedicalPatient medicalPatient ) throws AlipayApiException {
        JSONObject info = new JSONObject();
        info.put("patient_id",medicalPatient.getPatId());
        info.put("hospital_id", "szszyy");
        info.put("visit_date",medicalPatient.getVisitTime());
        info.put("medical_type", "O");
        info.put("case_no", "333333333333333333");
        info.put("serial_no", medicalPatient.getMzFeeId());
        String beforeEncryptData = info.toJSONString();
        logger.info("模拟支付宝回调加密前数据beforeEncryptData={}",beforeEncryptData);
        String afterEncryptData = AlipayEncrypt.encryptContent(beforeEncryptData, "AES", Constant.ENCRYPT_KEY, "utf8");
        JSONObject result = new JSONObject();
        result.put("content",afterEncryptData);
        return  result.toJSONString();
    }

    public void  reportTestOutHospital() throws AlipayApiException {
        String method = "yyt.sb.notify.out.hospital.report";
        MedicalPatient queryMedicalPatient = new MedicalPatient();
        queryMedicalPatient.setOrgCode("szszyy");
        queryMedicalPatient.setBusinessType(new Short("2"));
        List<MedicalPatient> medicalPatientList = medicalPatientDao.selectLimitPatient(queryMedicalPatient);
        for (MedicalPatient medicalPatient : medicalPatientList){
            Map<String ,String> requestMap =  new HashMap<String,String>();
            requestMap.put("hospital_code","szszyy");
            requestMap.put("in_patient_id",medicalPatient.getInpatientId());
            Response medicalResponse = SbService.callHumpFromInternal(method, JsonUtils.toJsonString(requestMap));
        }
    }





}
